package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.common.pojo.Announcement;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.HuanXinApi;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.UsersApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.HuanXinUser;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;

import com.tanhua.server.vo.AnnouncementVo;
import com.tanhua.server.vo.MessageCommentVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.UserInfoVo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IMService {

    @Reference(version = "1.0.0")
    private HuanXinApi huanXinApi;

    @Autowired
    private UserInfoService userInfoService;

    @Reference
    private UsersApi usersApi;

    @Reference
    private QuanZiApi quanZiApi;

    @Autowired
    private AnnouncementService announcementService;

    public UserInfoVo queryUserInfoByUserName(String userName) {
        //查询环信账户
        HuanXinUser huanXinUser = this.huanXinApi.queryUserByUserName(userName);
        if (ObjectUtil.isEmpty(huanXinUser)) {
            return null;
        }

        //查询用户信息
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(huanXinUser.getUserId());
        if (ObjectUtil.isEmpty(userInfo)) {
            return null;
        }

        UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class, "marriage");
        userInfoVo.setGender(userInfo.getSex().toString().toLowerCase());
        userInfoVo.setMarriage(StrUtil.equals("已婚", userInfo.getMarriage()) ? 1 : 0);

        return userInfoVo;
    }


    /**
     * 添加好友
     * @param friendId
     * @return
     */
    public boolean contactUser(Long friendId) {
        User user = UserThreadLocal.get();
        String id = usersApi.saveUsers(user.getId(), friendId);

        if (StrUtil.isNotEmpty(id)) {
            //注册好友关系到环信
            return huanXinApi.addUserFriend(user.getId(), friendId);
        }
        return false;
    }

    /**
     * 查看关系列表
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResult<Users> queryContactsList(Integer page, Integer pageSize, String keyword) {
        User user = UserThreadLocal.get();
        //usersApi.
         return null;

     ///   return  usersApi.queryUsersList(user.getId(), page, pageSize);
    }

    /**
     * 查询点赞列表
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult queryLikeCommentList(Integer page, Integer pageSize) {
        Long userId = UserThreadLocal.get().getId();
        PageInfo<Comment> pageInfo = quanZiApi.queryLikeCommentListByUser(userId, page, pageSize);

    }

    /**
     * 查询评论列表
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult queryUserCommentList(Integer page, Integer pageSize) {
    }

    public PageResult queryLoveCommentList(Integer page, Integer pageSize) {
    }

    //pageInfo转换为pageResult的方法
    PageResult transfor (PageInfo<Comment> pageInfo) {
        PageResult pageResult = new PageResult();
        pageResult.setPagesize(pageInfo.getPageSize());
        pageResult.setPage(pageInfo.getPageNum());
        List<Comment> records = pageInfo.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            //没有查询到数据
            return pageResult;
        }
        //查询到数据
        //所有的给当前作者点过赞的用户idlist
        List<Object> userId = CollUtil.getFieldValues(records, "userId");
        //通过userId查找到UserInfo
        List<UserInfo> userInfos = userInfoService.queryUserInfoByUserIdList(userId);

        List<MessageCommentVo> messageCommentVoList = new ArrayList<MessageCommentVo>();
        for (Comment comment : records) {
            for (UserInfo userInfo : userInfos) {
                if (ObjectUtil.equal(comment.getUserId(), userInfo.getUserId())) {
                    MessageCommentVo messageCommentVo = new MessageCommentVo();
                    messageCommentVo.setId(comment.getId().toHexString());
                    messageCommentVo.setAvatar(userInfo.getLogo());
                    messageCommentVo.setNickname(userInfo.getNickName());
                    messageCommentVo.setCreateDate(DateUtil.format(new Date(comment.getCreated()), "yyyy-MM-dd HH:mm"));
                    messageCommentVoList.add(messageCommentVo);
                }

            }

        }
    }

//            获取给定Bean列表中指定字段名对应字段值的列表<br>
//	  列表元素支持Bean与Map
//        CollUtil.getFieldValues()
//           return pageResult;
//    }

    public PageResult queryMessageAnnouncementList(Integer page, Integer pageSize) {
        announcementService.queryList(page, pageSize)
//        IPage<Announcement> announcementPage = announcementService.queryList(page, pageSize);

        List<AnnouncementVo> announcementVoList = new ArrayList<>();

        for (Announcement record : announcementPage.getRecords()) {
            AnnouncementVo announcementVo = new AnnouncementVo();
            announcementVo.setId(record.getId().toString());
            announcementVo.setTitle(record.getTitle());
            announcementVo.setDescription(record.getDescription());
            announcementVo.setCreateDate(DateUtil.format(record.getCreated(), "yyyy-MM-dd HH:mm"));

            announcementVoList.add(announcementVo);
        }

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        pageResult.setItems(announcementVoList);

        return pageResult;
    }
}